package thoven.achievement.net.jsonutil.ui

import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.treeStructure.Tree
import java.awt.BorderLayout
import javax.swing.JPanel
import javax.swing.tree.DefaultMutableTreeNode
import javax.swing.tree.DefaultTreeModel

/**
 * JSON 树形视图组件
 */
class JsonTreeViewer : JPanel(BorderLayout()) {
    
    private val tree: Tree
    private val treeModel: DefaultTreeModel
    private val rootNode: DefaultMutableTreeNode
    
    init {
        rootNode = DefaultMutableTreeNode("JSON")
        treeModel = DefaultTreeModel(rootNode)
        tree = Tree(treeModel)
        tree.isRootVisible = true
        tree.showsRootHandles = true
        
        add(JBScrollPane(tree), BorderLayout.CENTER)
    }
    
    /**
     * 显示 JSON 内容
     */
    fun displayJson(json: String) {
        try {
            rootNode.removeAllChildren()
            
            val jsonElement = JsonParser.parseString(json)
            buildTree(rootNode, jsonElement, "root")
            
            treeModel.reload()
            expandAllNodes()
        } catch (e: Exception) {
            rootNode.removeAllChildren()
            rootNode.add(DefaultMutableTreeNode("错误: ${e.message}"))
            treeModel.reload()
        }
    }
    
    /**
     * 清空树形视图
     */
    fun clear() {
        rootNode.removeAllChildren()
        rootNode.userObject = "JSON"
        treeModel.reload()
    }
    
    /**
     * 递归构建树节点
     */
    private fun buildTree(parentNode: DefaultMutableTreeNode, element: JsonElement, key: String) {
        when {
            element.isJsonObject -> {
                val objNode = DefaultMutableTreeNode("$key: { }")
                parentNode.add(objNode)
                
                val jsonObject = element.asJsonObject
                jsonObject.entrySet().forEach { (k, v) ->
                    buildTree(objNode, v, k)
                }
            }
            element.isJsonArray -> {
                val arrayNode = DefaultMutableTreeNode("$key: [ ]")
                parentNode.add(arrayNode)
                
                val jsonArray = element.asJsonArray
                jsonArray.forEachIndexed { index, item ->
                    buildTree(arrayNode, item, "[$index]")
                }
            }
            element.isJsonPrimitive -> {
                val primitive = element.asJsonPrimitive
                val value = when {
                    primitive.isString -> "\"${primitive.asString}\""
                    primitive.isNumber -> primitive.asNumber.toString()
                    primitive.isBoolean -> primitive.asBoolean.toString()
                    else -> primitive.toString()
                }
                parentNode.add(DefaultMutableTreeNode("$key: $value"))
            }
            element.isJsonNull -> {
                parentNode.add(DefaultMutableTreeNode("$key: null"))
            }
        }
    }
    
    /**
     * 展开所有节点
     */
    private fun expandAllNodes() {
        for (i in 0 until tree.rowCount) {
            tree.expandRow(i)
        }
    }
}

